/**  
  -- The Performance Driven Lime Project -- 

  A self adaptive tuple space for mobile agents and hosts by dynamic
  adapting tuple's replication policy.
  
  Copyright (C) 2008
  Xing Jiankuan, Pan Wenhui
  SAGroup of Tsinghua University

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */
package sagroup.dracots.policy;

/**
 * The policy that guide replications' creation and deletion.<br>
 * Two sub category of policies are defined here:<br>
 * <ul>
 * <li><b>Replication Policy: </b> Control whether replication should be
 * created. including FR, RC and SO.</li>
 * <li><b>Update Policy: </b> Control how to acquire replica's update.</li>
 * </ul>
 * This policy is populated through policy tuple. Both policies are applied to
 * one type of tuples on the current tuple space only.
 * 
 * @author Huang Xiaodong
 * 
 */
@SuppressWarnings("serial")
public class ReplicationPolicy implements java.io.Serializable {

	final byte policyValue;

	/**
	 * unknown policy
	 */
	public static final ReplicationPolicy UNKNOWN = new ReplicationPolicy((byte)0);

	// replication policy constants.
	/**
	 * Full Replication, which means any host has a complete set of tuples of a
	 * type, masters or replicas.
	 */
	public static final ReplicationPolicy FR = new ReplicationPolicy((byte)1);
	/**
	 * Store Originally, which means no replica is available, just as
	 * conventional Lime.
	 */
	public static final ReplicationPolicy SO = new ReplicationPolicy((byte)2);
	/**
	 * Read and Cache, which means after reading a tuple, create a replica
	 * locally for future use. If the tuple is updated in master site, it will push to
	 * the replica sites.
	 */
	public static final ReplicationPolicy RC = new ReplicationPolicy((byte)3);
	/**
	 * Cache once, which the tuple is only cached once in the replica site. 
	 * If the master updates the tuple, it will delete the replica in remote 
	 * hosts directly.    
	 */
	public static final ReplicationPolicy CO = new ReplicationPolicy((byte)4);

	public ReplicationPolicy(byte replicationPolicy) {
		this.policyValue = replicationPolicy;
	}

	public byte getReplicationPolicy() {
		return policyValue;
	}

	public String toString() {
		switch (policyValue) {
		case 1:
			return "FR";
		case 2:
			return "SO";
		case 3:
			return "RC";
		case 4:
			return "CO";
		default:
			return "UNKNOWN";
		}
	}

	public boolean equals(Object o) {
		if(this == o) return true;
		if(o instanceof ReplicationPolicy) {
			return this.policyValue == ((ReplicationPolicy) o).policyValue;
		} else {
			return false;
		}
	}
}